﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace CS_BEGINNER_TO_PROFICIENT.STD.ReflectionSTD
{
    public class MyClass2
    {
        public string MyProperty { get; set; }
        public int PublicField = 10;
        private string PrivateField = "Hello";

        public void Method1() { }
        public int Method2() { return 0; }

        private string PrivateMethod()
        {
            return "Hello from private method";
        }

        public void DoSomething()
        {
            Console.WriteLine("Doing something...");
        }
    }

    public class ReflectionSTDP2
    {
        public static void Execute()
        {
            Type type = typeof(MyClass2);
            object instance = Activator.CreateInstance(type);

            MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
            foreach (var method in methods)
            {
                Console.WriteLine($"Method: {method.Name}, Return Type: {method.ReturnType}");
            }

            PropertyInfo property = type.GetProperty("MyProperty");
            property.SetValue(instance, "Hello Reflection");
            string value = property.GetValue(instance) as string;
            Console.WriteLine($"Property Value: {value}");

            MethodInfo methodInfo = type.GetMethod("PrivateMethod",
                BindingFlags.NonPublic | BindingFlags.Instance);
            var result = methodInfo.Invoke(instance, null);
            Console.WriteLine($"Private Method Result: {result}");

            MethodInfo methodInfo1 = type.GetMethod("DoSomething");
            methodInfo1.Invoke(instance, null);

            FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (var field in fields)
            {
                var value1 = field.GetValue(instance);
                Console.WriteLine($"Field: {field.Name}, Value: {value1}");
            }


        }
    }
}
